/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.example.demotestoh.bean;

import com.example.demotestoh.slice.MainAbilitySlice;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

public class SockerTest extends Thread{

    private static final HiLogLabel hilog = new HiLogLabel(HiLog.DEBUG ,0x0000, "test0002");
    Socket socket = null;
    InputStream inputStream = null;
    OutputStream outputStream=null;
    boolean flag;


    MainAbilitySlice mains = null;

    public SockerTest(MainAbilitySlice mains) {
        this.mains = mains;
    }

    public void sockerSend(int a,int b,int c){
        if(socket != null && outputStream != null)
        {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        byte[] data =  new byte[4];
                        data[0] = (byte) 0xaa;
                        data[1] = (byte) a;
                        data[2] = (byte) b;
                        data[3] = (byte) c;
                        outputStream.write(data);
                        outputStream.flush();
                    } catch (IOException e) {
                        HiLog.debug(hilog,"erorr!"+e,"");
                    }

                }
            }).start();
        }else{

        }

    }

    @Override
    public void run() {
        try {
            flag = true;
            while (flag)
            {
                socket = new Socket("192.168.0.159", 18000);
                outputStream = socket.getOutputStream();
                inputStream =socket.getInputStream();
                init1();
                socket.close();
            }
        } catch (IOException e) {
            System.out.println("net erorr!"+e);
        }
    }

    public void dataProcessing(int n,byte[] b)
    {
        byte[] data = new byte[n];
        for (int i=0;i<n;i++)
        {
            data[i] = b[i];
        }
        mains.sockerCallback(data);
    }

    public void init1() {

        try {
            byte[] b = new byte[1024];
            while (flag)
            {
                int n = inputStream.read(b);
                if(n !=-1)
                {
                    dataProcessing(n,b);
                }else
                {
                    break;
                }
            }
        }catch (Exception e){
            HiLog.debug(hilog,"net erorr!"+e,"");
        }
    }

}
